package com.zhuoan.im.webapp.biz.impl;

import com.zhuoan.im.webapp.biz.FriendsApplyBiz;
import com.zhuoan.im.webapp.biz.GroupMemberBiz;
import com.zhuoan.im.webapp.biz.UserGroupBiz;
import com.zhuoan.im.webapp.common.FriendsApplyContant;
import com.zhuoan.im.webapp.common.UserGroupContant;
import com.zhuoan.im.webapp.dao.mapper.FriendsApplyInfoMapper;
import com.zhuoan.im.webapp.dao.model.FriendsApplyInfo;
import com.zhuoan.im.webapp.dao.model.GroupMember;
import com.zhuoan.im.webapp.dao.model.UserGroup;
import com.zhuoan.im.webapp.dao.model.reqbody.FriendsApplyBody;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * FriendsApplyBizImpl
 *
 * @author weixiang.wu
 * @date 2018-10-17 18:31
 **/
@Service
public class FriendsApplyBizImpl implements FriendsApplyBiz {

    private final static Logger logger = LoggerFactory.getLogger(FriendsApplyBizImpl.class);

    @Resource
    private FriendsApplyInfoMapper friendsApplyInfoMapper;

    @Resource
    private UserGroupBiz userGroupBiz;

    @Resource
    private GroupMemberBiz groupMemberBiz;


    @Override
    public void addUser(FriendsApplyBody friendsApplyBody) {
        FriendsApplyInfo friendsApplyInfo = new FriendsApplyInfo();
        friendsApplyInfo.setAppliedUserId(friendsApplyBody.getAppliedUserId());
        friendsApplyInfo.setApplyUserId(friendsApplyBody.getApplyUserId());
        String applyInfo = friendsApplyBody.getApplicationInformation();
        friendsApplyInfo.setApplicationInformation(StringUtils.isBlank(applyInfo) ? "对方请求添加你为好友" : applyInfo);
        friendsApplyInfo.setGmtCreate(new Date());
        friendsApplyInfo.setOperate(FriendsApplyContant.OPERATE_APPLYING);
        friendsApplyInfoMapper.insertSelective(friendsApplyInfo);
    }

    @Override
    public List<FriendsApplyInfo> queryUserApplyList(Long appliedUserId) {
        return friendsApplyInfoMapper.queryUserApplyList(appliedUserId);
    }

    @Override
    public void operateSingleUserApply(FriendsApplyBody friendsApplyBody) {
        FriendsApplyInfo friendsApplyInfo = new FriendsApplyInfo();
        //申请人
        Long applyUserId = friendsApplyBody.getApplyUserId();
        friendsApplyInfo.setApplyUserId(applyUserId);
        //被申请人
        Long appliedUserId = friendsApplyBody.getAppliedUserId();
        friendsApplyInfo.setAppliedUserId(appliedUserId);

        List<FriendsApplyInfo> friendsApplyInfos = friendsApplyInfoMapper.queryUserApply(friendsApplyInfo);
        if (friendsApplyInfos.size() > 0) {
            if (friendsApplyInfos.get(0).getOperate().equals(FriendsApplyContant.OPERATE_APPLYING)) {
                //当记录为申请中的时候，才去 同意or拒绝or忽略
                Integer operate = friendsApplyBody.getOperate();
                friendsApplyInfo.setOperate(operate);
                friendsApplyInfoMapper.updateFriendsApply(friendsApplyInfo);
                //倘若是“同意”的话，将申请人添加到“我的好友”群组
                if (operate.equals(FriendsApplyContant.OPERATE_ACCEPT)) {
                    //查找被申请人的 我的好友 群组 id，
                    UserGroup userGroup = userGroupBiz.queryGroupByCreatorUserIdAndType(appliedUserId,UserGroupContant.MY_FRIENDS);
                    //把申请人放到 我的好友组内
                    GroupMember groupMember = new GroupMember();
                    groupMember.setGmtCreate(new Date());
                    groupMember.setGroupId(userGroup.getGroupId());
                    groupMember.setInGroupUserId(applyUserId);
                    int i = groupMemberBiz.addGroupMember(groupMember);
                    if (i != 1) {
                        logger.error("好友添加发生异常");
                    }
                }
            }
        }
    }
}
